<template>
  <div>
    <common-head title="编辑联系人"></common-head>
     <div class="wrap">
      <van-address-edit
        :address-info="addressInfo"
        :area-list="areaList"
        show-postal
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="delLinkman"
      />
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { areaList } from '@vant/area-data'
import { editLinkmen, defaultLinkman, delLinkman } from '@api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      areaList,
      addressInfo: {

      }
    }
  },
  components: {
    CommonHead
  },
  created () {
    this.defaultLinkman()
  },
  methods: {
    defaultLinkman () {
      defaultLinkman({
        id: this.$route.query.id,
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.code === 0) {
          const obj = {}
          const linkman = res.data.data.info
          for (const key in linkman) {
            switch (key) {
              case 'address':
                obj.addressDetail = linkman[key]
                break
              case 'id':
                obj.id = linkman[key]
                break
              case 'linkMan':
                obj.name = linkman[key]
                break
              case 'mobile':
                obj.tel = linkman[key]
                break
              case 'isDefault':
                obj.isDefault = linkman[key]
                break
              case 'cityStr':
                obj.city = linkman[key]
                break
              default:
                break
            }
          }
          this.addressInfo = obj
        }
      })
    },
    onSave (linkman) {
      // 点击提交联系人
      const obj = {}
      for (const key in linkman) {
        switch (key) {
          case 'addressDetail':
            obj.address = linkman[key]
            break
          case 'areaCode':
            obj.cityId = linkman[key]
            break
          case 'name':
            obj.linkMan = linkman[key]
            break
          case 'tel':
            obj.mobile = linkman[key]
            break
          case 'postalCode':
            obj.provinceId = linkman[key]
            break
          default:
            break
        }
      }
      obj.isDefault = linkman.isDefault
      obj.id = this.$route.query.id
      editLinkmen({
        ...obj,
        token: localStorage.getItem('token')
      }).then(res => {
        Toast({
          type: 'success',
          message: '编辑联系人成功',
          onClose: () => {
            this.$router.push('/showLinkman')
          }
        })
      })
    },
    delLinkman () {
      delLinkman({
        id: this.$route.query.id,
        token: localStorage.getItem('token')
      }).then(res => {
        this.$router.push('/showLinkman')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 50px;
  background-color: #fff;
}
</style>
